package com.hundsun.cprs.yyt.service.match.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author fish
 * 
 */
public abstract class TradeMatch {

	public static final int PriceTypeSellPrice = 1;// 卖家价格

	public static final int PriceTypeBuyPrice = 2;// 买家价格

	private String id;

	private long matchSeq;// 撮合序号

	private Date matchTime;// 撮合时间

	private String stockCode;

	private Integer priceType;// 撮合价格类型

	private List<TradeMatchDetail> matchDetails = new ArrayList<TradeMatchDetail>();// 撮合的订单

	public TradeMatch() {
		super();
		matchTime = new Date();
	}

	public TradeMatch(String stockCode) {
		this();
		this.stockCode = stockCode;
	}

	public void addMatchDetail(TradeMatchDetail detail) {
		this.matchDetails.add(detail);
		detail.setMatch(this);
	}

	/**
	 * 得到本次成交的笔数,以买卖双方交易数大的一方为准
	 * 
	 * @return
	 */
	public int getTransactions() {
		int sell = 0, buy = 0;
		for (TradeMatchDetail matchDetail : this.matchDetails) {
			if (matchDetail.getEntrust().isBuy()) {
				buy++;
			} else {
				sell++;
			}
		}
		return Math.max(buy, sell);
	}

	/**
	 * 得到本次撮合总成交的份额数
	 * 
	 * @return
	 */
	public long getStockAmount() {
		long amount = 0;
		for (TradeMatchDetail matchDetail : this.matchDetails) {
			if (matchDetail.getEntrust().isBuy()) {
				amount += matchDetail.getMatchStockAmount();
			}
		}
		return amount;
	}

	/**
	 * 本次撮合总收取佣金数
	 * 
	 * @return
	 */
	public long getCommissionAmount() {
		long amount = 0;
		for (TradeMatchDetail matchDetail : this.matchDetails) {
			amount += matchDetail.getMatchCommission();
		}
		return amount;
	}

	/**
	 * 一次撮合完成，计算数据
	 */
	public void calculate() {
		for (TradeMatchDetail entrustMatch : this.getMatchDetails()) {
			entrustMatch.calculate();
		}
	}

	/**
	 * 是否以卖价成交
	 * 
	 * @return
	 */
	public boolean isSellPriceMatch() {
		return this.priceType != null
				&& this.priceType.intValue() == PriceTypeSellPrice;
	}

	/**
	 * 是否以买价成交
	 * 
	 * @return
	 */
	public boolean isBuyPriceMatch() {
		return this.priceType != null
				&& this.priceType.intValue() == PriceTypeBuyPrice;
	}

	/**
	 * 本次交易总的交易金额
	 * 
	 * @return
	 */
	public abstract long getFundAmount();

	/**
	 * 得到撮合类型
	 * 
	 * @return
	 */
	// public abstract int getMatchType();

	/**
	 * 本次撮合最高价
	 * 
	 * @return
	 */
	public abstract long getMaxPrice();

	/**
	 * 本次撮合最底价
	 * 
	 * @return
	 */
	public abstract long getMinPrice();

	/**
	 * 本次撮合最近价
	 * 
	 * @return
	 */
	public abstract long getLastPrice();

	/**
	 * 得到撮合价格
	 * 
	 * @return
	 */
	public abstract long getMatchPrice();

	public long getMatchSeq() {
		return matchSeq;
	}

	public void setMatchSeq(long matchSeq) {
		this.matchSeq = matchSeq;
	}

	public List<TradeMatchDetail> getMatchDetails() {
		return matchDetails;
	}

	public void setMatchDetails(List<TradeMatchDetail> matchDetails) {
		this.matchDetails = matchDetails;
	}

	public Date getMatchTime() {
		return matchTime;
	}

	public Integer getPriceType() {
		return priceType;
	}

	public void setPriceType(Integer priceType) {
		this.priceType = priceType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

    public String getStockCode() {
        return stockCode;
    }
}
